//
//  ViewController.m
//  XiMaLaYa
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 L-G. All rights reserved.
//

#import "LGCDiscoverPageViewController.h"
#import "TitleCollectionReusableView.h"
#import "EditorInforCollectionViewCell.h"
#import "LGCInforViewController.h"
#import "LGCLastViewController.h"
#import "LGCSortInforViewController.h"
#import "SearchViewController.h"
#import "SDCycleScrollView.h"
#import "FocusImages.h"
#import "GetConnection.h"
#import <MJRefresh.h>
#import "RESideMenu.h"

#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375

#define kTag 100

@interface LGCDiscoverPageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,SortTitleDelegate,SDCycleScrollViewDelegate>
// 创建横向collectionView
@property (nonatomic, retain)UICollectionView *collectionView;

@property (nonatomic, retain)NSMutableArray *arrayData;

@property (nonatomic, retain)NSMutableArray *arrayTitle;

@property (nonatomic, retain)NSMutableArray *arrayLoopsView;

@property (nonatomic, copy)NSString *homePath;

@end
@implementation LGCDiscoverPageViewController
- (void)dealloc
{
    [_arrayData release];
    [_arrayLoopsView release];
    [_arrayTitle release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"发现";
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"iconfont-liebiao"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 30, 30);
    [btn addTarget:self action:@selector(presentLeftViewController:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.arrayData = [NSMutableArray array];
    self.arrayTitle = [NSMutableArray array];
    self.arrayLoopsView = [NSMutableArray array];
    [self createSubviews];

    // 搜索
    UIButton *btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
    btnSearch.frame = CGRectMake(0, 0, 30 * WIDTH, 30 * HEIGHT);
    [btnSearch setImage:[UIImage imageNamed:@"iconfont-sousuo"] forState:UIControlStateNormal];
    [btnSearch addTarget:self action:@selector(btnSearch:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnSearch];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

-(void)presentLeftViewController:(id)sender
{
    [self.sideMenuViewController presentLeftMenuViewController];
}

// 创建collectionView
- (void)createSubviews
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 10 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 10 * HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:self.collectionView];
    [self.collectionView setContentInset:UIEdgeInsetsMake(180 * HEIGHT, 0, 0, 0)];
    [_collectionView release];
    
    // 注册cell
    [self.collectionView registerClass:[EditorInforCollectionViewCell class] forCellWithReuseIdentifier:@"editorCell"];
    
    // 注册头视图
    [self.collectionView registerClass:[TitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReusable"];
    
    // 注册尾视图
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerReusable"];
    
    [self addHeader];
}
#pragma mark 下拉刷新
- (void)addHeader
{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getconnection];
    }];
     [self.collectionView.mj_header beginRefreshing];
}

#pragma mark collectionView返回的各种属性
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5 * HEIGHT;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5 * HEIGHT;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10 * WIDTH, 5 * HEIGHT, 10 * WIDTH, 10 * HEIGHT);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(110 * WIDTH, 145 * HEIGHT);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *array = self.arrayData[section];
    return array.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.arrayData.count;
}
#pragma mark 返回collectionview的头视图和尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        TitleCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReusable" forIndexPath:indexPath];
        header.sortModel = self.arrayTitle[indexPath.section];
        header.section = indexPath.section;
        header.delegate = self;
        return header;
    }else
    {
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerReusable" forIndexPath:indexPath];
        footer.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return footer;
    }
}
#pragma mark 更多点击事件
- (void)sendSection:(NSInteger)section
{
    LGCSortInforViewController *sortInfor = [[LGCSortInforViewController alloc] init];
    SortModel *sortModel = self.arrayTitle[section];
    sortInfor.strID = sortModel.categoryId;
    sortInfor.strTitle = sortModel.title;
    [self.navigationController pushViewController:sortInfor animated:YES];
    [sortInfor release];
}
#pragma mark 返回头视图和尾视图的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 23 * HEIGHT);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 8 * HEIGHT);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    EditorInforCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"editorCell" forIndexPath:indexPath];
    EditorAlbums *editorAlbums = [[self.arrayData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.editorAlbums = editorAlbums;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    LGCInforViewController *inforVC = [LGCInforViewController shareLGCInformation];
    inforVC.strID = [[[self.arrayData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] albumId];
    [self.navigationController pushViewController:inforVC animated:YES];
}
#pragma mark 请求数据
- (void)getconnection
{
     NSString *str = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.20";
    [GetConnection startGETConnection:str parmaters:nil connectionBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *hotRecommend = [[dic objectForKey:@"hotRecommends"] objectForKey:@"list"];
        // 取出数据内容
        for (NSDictionary *temp in hotRecommend) {
            SortModel *sortModel = [SortModel sortModelWithDictionary:temp];
            [self.arrayTitle addObject:sortModel];
            // 初始化数组
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *dic in sortModel.list) {
                EditorAlbums *editorAlbums = [EditorAlbums editorAlbumsWithDictionary:dic];
                [array addObject:editorAlbums];
            }
            [self.arrayData addObject:array];
        }
        [self.collectionView reloadData];
    }];
    // 取出数据中的轮播图
    NSMutableArray *arrayPicUrl = [NSMutableArray array];
    NSString *str1 = @"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=1&contentType=album&device=iPhone&scale=2&version=4.3.20";
    [GetConnection startGETConnection:str1 parmaters:nil connectionBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *focusI = [[dic objectForKey:@"focusImages"] objectForKey:@"list"];
    for (NSDictionary *temp in focusI) {
        if ([temp.allKeys containsObject:@"uid"]) {
            FocusImages *focusImages = [FocusImages focusImagesWithDictionary:temp];
            if (focusImages.type == 2) {
            [arrayPicUrl addObject:focusImages.pic];
            [self.arrayLoopsView addObject:focusImages];
            }
        }
    }
    // 创建轮播图
    SDCycleScrollView *LoopsView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, - 180 * HEIGHT, self.view.frame.size.width, 180 * HEIGHT)imageURLStringsGroup:arrayPicUrl];
    LoopsView.delegate = self;
    [self.collectionView addSubview:LoopsView];
    }];
}
// 轮播图点击事件
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    FocusImages *focusImages = self.arrayLoopsView[index];
    LGCInforViewController *lgcInfor = [LGCInforViewController shareLGCInformation];
    lgcInfor.strID = focusImages.albumId;
    [self.navigationController pushViewController:lgcInfor animated:YES];
}
- (void)btnSearch:(id)sender
{
    SearchViewController *search = [[SearchViewController alloc] init];
    [search setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:search animated:YES];
    [search release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
